package myJs.user.workflow

import myJs.Implicits.JDS
import myJs.Utils._
import scala.scalajs.js.Dynamic.{global => g}
import myJs.tool.Tool
import myJs.tool.Tool._
import org.scalajs.dom.ext.Ajax
import myJs.Implicits._
import myJs.myPkg.jquery.{$, JQuery}
import scalatags.Text.all._

import scala.scalajs.js
import myJs.myPkg.bootstrap.Bootstrap.default._
import shared.Pojo._

import scala.collection.immutable.SeqMap
import scala.scalajs.js.JSON
import org.scalajs.dom.{Element, document}
import upickle.default._
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._

/**
 * Created by yz on 28/10/2020
 */
trait WorkflowTool {

  def registerEvent = {
    $(document.body).on("change", s".myReplaceMethod", "", (y: Element) => {
      replaceMethodChange(y)
    })
    $(document.body).on("change", s".myNormalMethod", "", (y: Element) => {
      normalMethodChange(y)
    })
    $(document.body).on("change", s".myTransformMethod", "", (y: Element) => {
      transformMethodChange(y)
    })
    $(document.body).on("change", s".myGlogMethod", "", (y: Element) => {
      glogMethodChange(y)
    })
    $(document.body).on("change", s".myKeepMethod", "", (y: Element) => {
      keepMethodChange(y)
    })

  }

  def replaceMethodChange(y: Element) = {
    val myValue = $(y).find(">option:selected").`val`().toString
    val parentJq = $(y).parents(".myEachArg")
    val knnJq = parentJq.find("#knn")
    val minJq = parentJq.find("#min")
    myValue match {
      case "knn" =>
        knnJq.show()
        minJq.hide()
      case x if List("min", "rowMin").contains(x) =>
        knnJq.hide()
        minJq.show()
      case _ =>
        knnJq.hide()
        minJq.hide()
    }
  }

  def normalMethodChange(y: Element) = {
    val myValue = $(y).find(">option:selected").`val`().toString
    val parentJq = $(y).parents(".myEachArg")
    val sumJq = parentJq.find("#sum")
    myValue match {
      case "sum" =>
        sumJq.show()
      case _ =>
        sumJq.hide()
    }
  }

  def transformMethodChange(y: Element) = {
    val myValue = $(y).find(">option:selected").`val`().toString
    val parentJq = $(y).parents(".myEachArg")
    val glogJq = parentJq.find("#glog")
    myValue match {
      case "glog" =>
        glogJq.show()
      case _ =>
        glogJq.hide()
    }
  }

  def glogMethodChange(y: Element) = {
    val myValue = $(y).find(">option:selected").`val`().toString
    val parentJq = $(y).parents(".myEachArg")
    val glogValueJq = parentJq.find("#glogValue")
    myValue match {
      case "custom" =>
        glogValueJq.show()
      case _ =>
        glogValueJq.hide()
    }
  }

  def keepMethodChange(y: Element) = {
    val myValue = $(y).find(">option:selected").`val`().toString
    val showJq = $(y).parents(".myEachArg").find(s"#${myValue}")
    showJq.siblings().hide()
    showJq.show()
  }

  def refreshCalculateConfig(f: JDJAn => Any = x => ()) = {
    val missionId = Tool.getMissionId
    if (missionId.nonEmpty) {
      val url = g.jsRoutes.controllers.WorkflowController.getConfigData().url.toString
      Ajax.get(url = s"${url.noCache}&missionId=${missionId}", headers = Map("Content-Type" -> "application/json")).map { xhr =>
        val rs = xhr.responseText.toJDJAn
        Tool.fillByNames(rs.toJDS)
        Tool.fillByNameAndTriggers(rs)
        $(".myFile").foreach { y =>
          val name = $(y).nameAttr
          $("#form").bv.revalidateField(name)
        }
        f(rs)
      }
    }
  }

  def getShowName(kind: String) = {
    val softF = Tool.kindWorkflowFMap(kind)
    messages(softF.nameStr)
  }

  def getKindA(kind: String) = {
    val url = getUrl(kind)
    val showName = getShowName(kind)
    a(href := s"${url}", showName)
  }

  def getUrl(kind: String) = {
    val softF = Tool.kindWorkflowFMap(kind)
    softF.url
  }

  lazy val kindShowNameMap = Tool.workflows.map { x =>
    (x.nameStr -> messages(x.nameStr))
  }.toMap

}
